﻿//审批时预算的柔性控制消息
window.FlexControlParam = null;
 
/**
 * 返回web报销单据状态值
 */
function jkbxGetBillStatusValue(billStatusName)
{
	// 审批未通过
	if(billStatusName == IBillStatusName.NOPASS)
		return 2;
	// 审批通过	
	else if(billStatusName == IBillStatusName.CHECKPASS)
		return 1;
	// 审批进行中	
	else if(billStatusName == IBillStatusName.CHECKGOING)
		return 0;
	// 提交状态	
	else if(billStatusName == IBillStatusName.COMMIT)
		return -1;
	// 作废状态	
	else if(billStatusName == IBillStatusName.DELETE)
		return 4;
	// 冲销状态	
	else if(billStatusName == IBillStatusName.CX)
		return 5;	
	// 终止(结算）态
	else if(billStatusName == IBillStatusName.ENDED)
		return 6;
	// 冻结状态
	else if(billStatusName == IBillStatusName.FREEZE)
		return 7;
	// 自由态
	else if(billStatusName == IBillStatusName.FREE)
		return 8;
	else if(billStatusName == IBillStatusName.ALL)
		return 30;		
	else if(billStatusName == IBillStatusName.REJECT)	
		return 3;
}; 
/*流程动作之前处理，重载PFBillEventHandler.js中方法*/
function jkbxBeforeAction(actionName, billType)
{ 
	var headDs = this.getMasterDs();
	var busitemDs = getSubDs();
	var djztIndex = headDs.nameToIndex('djzt');
	var flag = true;
	switch(actionName){
		case IPfAction.SAVE:
				if(headDs.id == 'bx_bxzb'){
					if(busitemDs != null && busitemDs.getRowCount() > 0 ){
						//业务信息页签的行数据变化对财务页签触发
						busItemTabRowChange1();
					}
				}
				headDs.setValueAt(headDs.getSelectedIndex(),djztIndex,IBillStatus.DJZT_TempSaved);
				dealZyMcToPK(headDs);
				//如果是暂存则将冲借款清空
				if(headDs.id == 'bx_bxzb'){
					if(!clernCjk())
						flag = false;
				}
				isSave = true;
				break;
		case IPfAction.APPROVE:
				break;
		case IPfAction.COMMIT:
				if(headDs.id == 'bx_bxzb'){
					if(busitemDs != null && busitemDs.getRowCount() > 0 ){
						busItemTabRowChange1();
					}
				}
				//校验
				if(!checkDs(headDs))
					return false;
				if(headDs.id == 'bx_bxzb'){
					if(!checkBusItemDs(busitemDs))
						return false;	
				}
				//清除业务页签中空行
				if(!deleteEmptyRow(busitemDs))
					return false;
				headDs.setValueAt(headDs.getSelectedIndex(),djztIndex,IBillStatus.DJZT_Saved);
				var isContrast = getSessionAttribute("IS_CONTRAST");
				if(headDs.id == 'bx_bxzb' && isContrast == 'Y'){
					if(!$beforeOpenCjkCheck())
						flag = false;
					/*判断借款人是否有未两清的借款单*/
					var value = getService("webbx_common_service").getJkd($getParamMap());
					var cjkybje = $f(headDs.getSelectedRow().getCellValue(headDs.nameToIndex('cjkybje')));
					if(value == 'Y' && billUI.eventHandler.noCjk){
						billUI.eventHandler.noCjk = true;//需要冲借款
						var dialog = ConfirmDialogComp.showDialog(webbx_trans('yer_jkbx003'),yesTodo,notTodo,null,null,null,null);
						flag =  false;
					}
					else
						flag = true;
				}else if(headDs.id == 'jk_jkzb'){
					var ybje = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('ybje'));
					//姚晡---云南信托--借款单据不判断金额为空或者为零
					//if(ybje == null || $f(ybje) == 0){
					//	showMessageDialog(webbx_trans('yer_jkbx004'));
					//	flag =  false;
					//}
				}else{
					flag = true;
				}
				//用验证公式来验证这两个Ds数据
				if(executeWebbxValidateFormular(headDs)){
					flag =  false;
				}
				if(busitemDs != null && executeWebbxValidateFormular(busitemDs))
					flag =  false;
				isSave = true;
				if(flag){
					dealZyMcToPK(headDs);
				}
				break;
		case IPfAction.DELETE:
				break;
		case IPfAction.EDIT:
				break;
		case IPfAction.UNAPPROVE:
				break;
	}
	return flag;
};

/**
 * 单据操作后处理，重载PFBillEventHandler.js中方法
 */
function jkbxAfterAction(actionName, billType)
{ 
	this.billUI.setTotalUIStatus(null,this.getBillBussStatus());
	var headDs = this.getMasterDs();
	var itemDs = getSubDs();
	dealZyPKToMc(headDs);
	switch(actionName){
		case IPfAction.SAVE:
				setBbhlActive(headDs);
				this.onBoRefresh();
				break;
		case IPfAction.APPROVE:
				break;
		case IPfAction.COMMIT:
				setBbhlActive(headDs);
				this.onBoRefresh();
				break;
		case IPfAction.DELETE:
				resetBzbmAfterAction(headDs);
				break;
		case IPfAction.EDIT:  
				this.onBoRefresh();
				break;
		case IPfAction.UNAPPROVE:
				//this.onBoRefresh();//注释for： 列表下多选， 反审后checkbox不能选了
				break;
	}
	return true;  
};

/*重写PFBillEventHandler*/
PFBillEventHandler.prototype.returnFunForCheck = function(returnArg, isSuccess, returndoc){
	if(isSuccess){
		var oThis = returnArg[0], headDs = returnArg[1], action = returnArg[2];
		// 如果没有审批流指派，则直接通过，不弹出框
		if(returndoc == 'NOPF'){
			if(action == "commit"){
				billUI.eventHandler.JkbxSaveImpl(null);
			}else if(action == "approve"){
				oThis.commitAudit("", "approve");// 审批通过
			}
		}
		else{
			var paramArr = decodeURIComponent(returndoc).split('#');
			returndoc = paramArr[0];
			window.FlexControlParam = paramArr[1]; 
			
			var meta = headDs.metadata;
			var key = null;
			for(var i = 0, count = meta.length; i < count; i++){
				if(meta[i].isPrimaryKey){
					key = meta[i].key;
					break;
				}
			}	
			//如果有指派信息，则将指派信息放入window参数
			var haveAssign = "N";
			window.assignparams = null;
			if("NO_ASSIGN" != returndoc){
				window.assignparams = returndoc;
				haveAssign = "Y";
			}
			
			if(action == "commit"){
				if(returndoc == 'NO_ASSIGN')
					billUI.eventHandler.JkbxSaveImpl(null);
				else{
					var url = getCorePath() + "/assign/assign.jsp?pageId=assign"
							  + "&action=" + action
							  + "&rand=" + (Math.random()*10000).toString().substring(0, 4);
					showDialog(url , trans('ml_assigninfo'), "600", "500", "assignable_info_dialog", false,true);
				}
			}
			else if(action == "approve"){
				var billId = headDs.getValue(key);
				var billType = oThis.billUI.billController.billType;
				var url = getCorePath() + "/approve/approve.jsp?pageId=approve&billType=" + billType + "&billId=" 
						+ billId + "&haveAssign=" + haveAssign;
				showDialog(url , trans('ml_approvedialog'), "600", "450", "approveDialogId", false);
			}
		}
	}
};


/*重写onBoAudit方法**/
function jkbxAudit(){
	
	var headDs = this.getMasterDs();
	var allSubDsArr = this.getAllSlaveDs();
	var sRow = headDs.getSelectedRow();
	if(sRow == null )
		showMessageDialog(trans('ml_noselrow'));
	// 判断单据状态,如果状态一致则退出
	if (this.checkVOStatus(this.getBillStatus(headDs, headDs.getSelectedRow()), [this.getBillStatusValue(IBillStatusName.CHECKPASS)])) 
	{
		showMessageDialog(trans('ml_billapproved'));
		return;
	}	
    if(this.beforeAction(IPfAction.APPROVE) == false)
		return;
	if(this.beforeOpenApproveDialog(headDs) == false)
		return;
	//获取指派信息
	//XXX 添加审批时预算的柔性控制  在校验是否指派时，为了少次调用
	this.checkIsAssign(headDs, allSubDsArr, "approve");
	
}